package com.clean.cleantotal.compores.process

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.clean.cleantotal.R
import com.clean.cleantotal.base.BaseActivity
import com.clean.cleantotal.bean.AppInfo
import com.clean.cleantotal.bean.DataUtils
import com.clean.cleantotal.bean.FunType
import com.clean.cleantotal.databinding.ActivityProcessBinding
import com.clean.cleantotal.compores.result.ResultActivity
import com.clean.cleantotal.utils.MyDeviceUtils
import com.clean.cleantotal.utils.Utils
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup

class ProcessActivity : BaseActivity<ActivityProcessBinding>() {
    override val viewBinding: ActivityProcessBinding
        get() = ActivityProcessBinding.inflate(layoutInflater)

    override fun initView() {
        mBinding.back.setOnClickListener { onBackPressed() }
        Utils.showScanDialog(this, FunType.PROCESS, mBinding.loadingLayout.scanLayout, {
            animal = it
        }) {

        }
        mBinding.includedLayout.yellowYuan.isVisible = true
        mBinding.includedLayout.tvSize.text = "${MyDeviceUtils.getMemoryPercent(this)}"
        mBinding.includedLayout.tvUnit.text = "%"

        DataUtils.getProcessList(lifecycleScope, this) {
            animal?.duration = 3000
            mBinding.recyclerView.linear().setup {
                addType<AppInfo> { R.layout.item_process }
                onClick(R.id.close) {
                    val intent = Intent()
                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    intent.setData(Uri.fromParts("package", getModel<AppInfo>().pkName, null))
                    startActivity(intent)
                }
            }.models = it
            mBinding.includedLayout.tvTitle.text = "Ram used"
            mBinding.includedLayout.tvContent.text =
                "${it.size} App(s) are running in the background."
            mBinding.includedLayout.ivFinish.isVisible = false
        }

        mBinding.okLayout.setOnClickListener {
            openActivity(ResultActivity::class.java, Bundle().apply {
                putSerializable("funType", FunType.PROCESS)
                finish()
            })
        }
    }

}